import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'function.dart';

class BlockHistory extends StatefulWidget {
  BlockHistory(this.mainColor);
  final Color mainColor;
  @override
  _BlockHistoryState createState() => new _BlockHistoryState();
}

class _BlockHistoryState extends State<BlockHistory> {
  List historyArr = [];
  _BlockHistoryState() {
    initHistory();
  }
  initHistory() async {
    var arr = await selectBlock();
    historyArr = arr ?? [];
    setState(() {});
  }

  Widget _buildTiles(Map history) {
    return new Column(
      children: <Widget>[
        new ListTile(
          onTap: () {
            Navigator.of(context).pop(history['addr']);
          },
          title: new Text(
            history['note']??history['addr'],
            style: new TextStyle(fontSize: 16.0, color: Colors.black),
            softWrap: true,
          ),
          subtitle: new Text(convertTime(history['last'], millisecond: true)),
          trailing: new Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              new IconButton(
                tooltip:"添加备注",
                icon: new Icon(
                  Icons.note_add,
                  color: Colors.black,
                ),
                onPressed: () {
                  showDialog<Null>(
                    context: context,
                    builder: (context) {
                      return new AlertDialog(
                        title: new Text("删除记录"),
                        content: new Text("确定要删除此查询记录？"),
                        actions: <Widget>[
                          new FlatButton(
                            onPressed: () async {
                              if (await deleteBlock(history['id'])) {
                                toast("删除成功");
                                Navigator.of(context).pop();
                                initHistory();
                              } else {
                                toast("删除失败，请重试");
                              }
                            },
                            child: new Text(
                              '删除',
                              style: new TextStyle(
                                color: Colors.red,
                              ),
                            ),
                          ),
                          new FlatButton(
                            onPressed: () {
                              Navigator.pop(context);
                            },
                            child: new Text(
                              '取消',
                            ),
                          ),
                        ],
                      );
                    },
                  );
                },
              ),
              new IconButton(
                tooltip: "删除记录",
                icon: new Icon(
                  Icons.delete_forever,
                  color: Colors.red,
                ),
                onPressed: () {
                  showDialog<Null>(
                    context: context,
                    builder: (context) {
                      return new AlertDialog(
                        title: new Text("删除记录"),
                        content: new Text("确定要删除此查询记录？"),
                        actions: <Widget>[
                          new FlatButton(
                            onPressed: () async {
                              if (await deleteBlock(history['id'])) {
                                toast("删除成功");
                                Navigator.of(context).pop();
                                initHistory();
                              } else {
                                toast("删除失败，请重试");
                              }
                            },
                            child: new Text(
                              '删除',
                              style: new TextStyle(
                                color: Colors.red,
                              ),
                            ),
                          ),
                          new FlatButton(
                            onPressed: () {
                              Navigator.pop(context);
                            },
                            child: new Text(
                              '取消',
                            ),
                          ),
                        ],
                      );
                    },
                  );
                },
              ),
            ],
          ),
        ),
        Divider(height: 1.0),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new CupertinoNavigationBar(
        actionsForegroundColor: Colors.white,
        backgroundColor: widget.mainColor,
        border: new Border(
          bottom: BorderSide.none,
        ),
        middle: new Text(
          "查询记录",
          style: new TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.normal,
          ),
        ),
      ),
      body: historyArr.length > 0
          ? ListView.builder(
        itemCount: historyArr.length,
        itemBuilder: (context, i) => _buildTiles(historyArr[i]),
      )
          : new Center(
        child: new Container(
          width: 200.0,
          height: 200.0,
          child: new Column(
            children: <Widget>[
              new Text(
                "暂无查询记录",
                style: new TextStyle(fontSize: 22.0),
              ),
              new FlatButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: new Text("返回")),
            ],
          ),
        ),
      ),
//          child:
    );
  }
}
